package com.wits.qso.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.CourseRegisterDaoCustom;
import com.wits.qso.entity.CourseRegister;

/**
 * 课程注册数据操作实现
 */
@Component
public class CourseRegisterDaoImpl extends BaseDaoImpl<CourseRegister, String>
		implements CourseRegisterDaoCustom {
	@SuppressWarnings("unchecked")
	public List<CourseRegister> findCourseRegisters(Map<String, Object> param) {
		if (param == null) {
			return new ArrayList<CourseRegister>();
		}
		// StringBuffer buf = new
		// StringBuffer(" select qcr.cr_id as id,qcr.stu_id as stuId,stu.stu_name as stuName,qcr.ter_id as terId,ter.ter_name as terName,qcr.course_id as courseId,course.course_title as courseName,qcr.cr_status as crStatus,qcr.cr_message as crMessage,qcr.date_apply as applyDate,qcr.date_auth as authDate from qso_course_register qcr left join qso_student stu on qcr.stu_id=stu.stu_id left join qso_teacher ter on qcr.ter_id=ter.ter_id left join qso_course course on qcr.course_id=course.course_id  where 1=1  ");
		// Map<String, String> names = CourseRegister.getTableColumnNames();
		// List<Object> pList = new ArrayList<Object>();
		// for (Iterator<String> it = names.keySet().iterator(); it.hasNext();)
		// {
		// String temp = it.next();
		// if (param.get(temp) != null) {
		// buf.append(" and ").append(temp).append(names.get(temp))
		// .append(param.get(temp));
		// pList.add(param.get(temp));
		// }
		// }
		StringBuffer buf = new StringBuffer(
				" select qcr.cr_id ,qcr.stu_id,stu.stu_name ,qcr.ter_id,ter.ter_name,qcr.course_id ,course.course_title,qcr.cr_status ,qcr.cr_message ,qcr.date_apply ,qcr.date_auth,qcr.remark,qcr.user_cre,qcr.date_cre,qcr.user_upd,qcr.date_upd  from qso_course_register qcr left join qso_student stu on qcr.stu_id=stu.stu_id left join qso_teacher ter on qcr.ter_id=ter.ter_id  left join qso_course course on qcr.course_id=course.course_id  where 1=1  ");

		if (param.get("cr_id") != null) {
			buf.append(" and qcr.cr_id=:cr_id ");
		}
		if (param.get("ter_id") != null) {
			buf.append(" and qcr.ter_id=:ter_id ");
		}
		if (param.get("ter_name") != null) {
			buf.append(" and ter.ter_name like %:ter_name% ");
		}
		if (param.get("ter_user_id") != null) {
			buf.append(" and ter.ter_userid =:ter_user_id ");
		}
		if (param.get("stu_user_id") != null) {
			buf.append(" and stu.stu_userid =:stu_user_id ");
		}
		if (param.get("stu_id") != null) {
			buf.append(" and qcr.stu_id =:stu_id ");
		}
		if (param.get("stu_name") != null) {
			buf.append(" and qcr.stu_name like %:stu_name% ");
		}
		if (param.get("course_id") != null) {
			buf.append(" and qcr.course_id =:course_id ");
		}
		if (param.get("course_title") != null) {
			buf.append(" and course.course_title like %:course_title% ");
		}
		if (param.get("cr_status") != null) {
			buf.append(" and qcr.cr_status =:cr_status ");
		}
		if (param.get("cr_not_status") != null) {
			buf.append(" and qcr.cr_status !=:cr_not_status ");
		}
		if (param.get("cr_in_status") != null) {
			String s=(String) param.get("cr_in_status");
			buf.append(" and qcr.cr_status in ( ").append(s).append(" ) ");
		}
		if (param.get("cr_message") != null) {
			buf.append(" and qcr.cr_message like %:cr_message% ");
		}
		if (param.get("date_apply") != null) {
			buf.append(" and qcr.date_apply =:date_apply ");
		}
		if (param.get("date_auth") != null) {
			buf.append(" and qcr.date_auth =:date_auth ");
		}

		Query a = em.createNativeQuery(buf.toString(), CourseRegister.class);
		if (param.get("cr_id") != null) {
			a.setParameter("cr_id", param.get("cr_id"));
		}
		if (param.get("ter_id") != null) {
			a.setParameter("ter_id", param.get("ter_id"));
		}
		if (param.get("ter_name") != null) {
			a.setParameter("ter_name", param.get("ter_name"));
		}
		if (param.get("ter_user_id") != null) {
			a.setParameter("ter_user_id", param.get("ter_user_id"));
		}
		if (param.get("stu_user_id") != null) {
			a.setParameter("stu_user_id", param.get("stu_user_id"));
		}
		if (param.get("stu_id") != null) {
			a.setParameter("stu_id", param.get("stu_id"));
		}
		if (param.get("course_id") != null) {
			a.setParameter("course_id", param.get("course_id"));
		}
		if (param.get("course_title") != null) {
			a.setParameter("course_title", param.get("course_title"));
		}
		if (param.get("cr_status") != null) {
			a.setParameter("cr_status", param.get("cr_status"));
		}
		if (param.get("cr_not_status") != null) {
			a.setParameter("cr_not_status", param.get("cr_not_status"));
		}
//		if (param.get("cr_in_status") != null) {
//			a.setParameter("cr_in_status", param.get("cr_in_status"));
//		}
		if (param.get("cr_message") != null) {
			a.setParameter("cr_status", param.get("cr_message"));
		}
		if (param.get("date_apply") != null) {
			a.setParameter("date_apply", param.get("date_apply"));
		}
		if (param.get("date_auth") != null) {
			a.setParameter("date_auth", param.get("date_auth"));
		}
		@SuppressWarnings("rawtypes")
		List list = a.getResultList();
		return list;
	}

	/**
	 * 
	 */
	public boolean modifyStatus(String id, String status) {
		boolean res=false;
		try {
			String sql = " update qso_course_register set cr_status=:cr_status where cr_id=:cr_id ";
			Query query = em.createNativeQuery(sql);
			query.setParameter("cr_id", id);
			query.setParameter("cr_status", status);

			query.executeUpdate();
			res=true;
		} catch (Exception e) {
			e.printStackTrace();
			res=false;
		}
		return res;
	}

	public boolean modifyFeild(Map<String, Object> param) {
		boolean res=false;
		try {
//			String sql = " update qso_course_register set cr_status=:cr_status,cr_message=:cr_message where cr_id=:cr_id ";
			int num=0;
			StringBuffer buf=new StringBuffer();
			if(param==null||param.size()==0||(param.containsKey("cr_id")&&param.size()==1)){
				throw new RuntimeException("can not find columns to update from given params!");
			}
			buf.append(" update qso_course_register set ");
			if(param.get("cr_status")!=null){
				num++;
				if(num>1){
					buf.append(",");
				}
				buf.append(" cr_status=:cr_status ");
			}
			if(param.get("cr_message")!=null){
				num++;
				if(num>1){
					buf.append(",");
				}
				buf.append(" cr_message=:cr_message ");
			}
			if(param.get("date_apply")!=null){
				num++;
				if(num>1){
					buf.append(",");
				}
				buf.append(" date_apply=:date_apply ");
			}
			if(param.get("date_auth")!=null){
				num++;
				if(num>1){
					buf.append(",");
				}
				buf.append(" date_auth=:date_auth ");
			}
			buf.append(" where cr_id=:cr_id ");
			
//			String sql = "  cr_status=:cr_status,cr_message=:cr_message ";
			Query query = em.createNativeQuery(buf.toString());
			query.setParameter("cr_id", param.get("cr_id"));
			if(param.get("cr_status")!=null){
				query.setParameter("cr_status", param.get("cr_status"));
			}
			if(param.get("cr_message")!=null){
				query.setParameter("cr_message", param.get("cr_message"));
			}
			if(param.get("date_apply")!=null){
				query.setParameter("date_apply", param.get("date_apply"));
			}
			if(param.get("date_auth")!=null){
				query.setParameter("date_auth", param.get("date_auth"));
			}
//			query.setParameter("cr_status", param.get("cr_status"));
//			query.setParameter("cr_message", param.get("cr_message"));

			query.executeUpdate();
			res=true;
		} catch (Exception e) {
			e.printStackTrace();
			res=false;
		}
		return res;
	}

}
